package bigbanksystemserver;

import java.awt.Point;
import java.awt.Window;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;
import javax.swing.JComponent;

/**
 *
 * @author Wong Kah Chun & Woo Chee Meng
 */
public class DraggableFrame {

    private Point clickPosition;
    private Window windowToMove;
    private JComponent componentToDrag;

    public DraggableFrame(final Window windowToMove, final JComponent componentToDrag) {
        this.windowToMove = windowToMove;
        this.componentToDrag = componentToDrag;

        componentToDrag.addMouseListener(new MouseAdapter() {
            public void mousePressed(MouseEvent e) {
                clickPosition = e.getPoint();
                windowToMove.getComponentAt(clickPosition);
            }
        });

        componentToDrag.addMouseMotionListener(new MouseMotionAdapter() {
            @Override
            public void mouseDragged(MouseEvent e) {

                // Get the location of frame
                int currentX = windowToMove.getLocation().x;
                int currentY = windowToMove.getLocation().y;

                // Determine how much the mouse moved since the initial click
                int xDistance = (currentX + e.getX()) - (currentX + clickPosition.x);
                int yDistance = (currentY + e.getY()) - (currentY + clickPosition.y);

                // Move window to this position
                int newX = currentX + xDistance;
                int newY = currentY + yDistance;
                windowToMove.setLocation(newX, newY);
            }
        });
    }
}
